# -*- coding: utf-8 -*-
import os
import logging.config
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOG_DIR = os.path.join(BASE_DIR, 'Log/')
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '[%(asctime)s] %(levelname)s : %(message)s'
        },
        'verbose': {
            'format': '[%(asctime)s] %(levelname)s %(module)s %(process)d %(thread)d : %(message)s'
        },
        'standard': {
            'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(levelname)s]-%(message)s'
        },
        'info': {
            'format': '[%(asctime)s] [%(levelname)s] %(message)s'
        },
        'error-http': {
            'format': '[%(asctime)s] [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(levelname)s] %(message)s'
        },
    },
    'handlers': {
        # 'default': {
        #     'level': 'DEBUG',
        #     'class': 'logging.handlers.RotatingFileHandler',
        #     'filename': os.path.join(LOG_DIR, 'default.log'),
        #     'maxBytes': 1024 * 1024 * 5,
        #     'backupCount': 5,
        #     'formatter': 'simple',
        # },
        'request_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'request.log'),
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 5,
            'formatter': 'standard',
        },
        'request': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'request.log'),
            'maxBytes': 1024 * 1024 * 50,
            'backupCount': 5,
            'formatter': 'simple',
        },
        'console_error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'console.log'),
            'formatter': 'standard',
        },
        'file-error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIR, 'error.log'),
            'formatter': 'standard',
            'maxBytes': 1024 * 1024 * 500,  # 5 MB
            'backupCount': 5,
            'mode': 'a',
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'info',
            'filename': os.path.join(LOG_DIR, 'info.log'),
            'maxBytes': 1024 * 1024 * 500,  # 5 MB
            'backupCount': 5,
            'mode': 'a',
        },
        'file-http': {
            'level': 'INFO',
            'class': 'logging.handlers.HTTPHandler',
            'formatter': 'info',
            "host": "127.0.0.1:8080",
            "url": "/log_server",
            "method": "POST",
        },
    },

    'loggers': {
        'docker_cloud_manage_info': {
            'handlers': ['file'],
            'level': 'INFO',
            'propagate': True,
        },
        'docker_cloud_manage_request': {
            'handlers': ['request'],
            'level': 'INFO',
            'propagate': True,
        },
        'http_request': {
            'handlers': ['console_error'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'ERROR',
            'propagate': False,
        },
    }

}

LOGGING_META = {
    'test': '%(name)s is %(years)d years old.',
    'module': '[%(module_text)s] %(module)s ',
    'handle': '[%(handle_text)s] %(handle)s',
    'method': '[%(method_text)s] %(method)s',
    # 请求报文协议相关的日志
    'request': {
        'method': '[RequestMethod] %(method)s ',
        'remote_ip': '[RemoteIp] %(remote_ip)s ',
        'protocol': '[Protocol] %(protocol)s ',
        'version': '[ProtoVersion] %(version)s ',
        'uri': '[URI] %(uri)s '
    },
    'result': {
        'code': '[Result Code] %(code)s ',
        'text': '[Result Text] %(text)s ',
        'node': '[Node] %(ip)s ',
        'user': '[User] %(user)s ',
    },
    'data_base': {
        'sql_detail': '[Service Name] %(service)s ',
    },
    'service-main': {
        'sevice': '[Service] %(service)s ',
        'method': '[Handle] %(service)s ',
    }

}
LOGGING_TEMPLATE = {
    'request_template_01': LOGGING_META['request']['protocol'] + LOGGING_META['request']['method']
        + LOGGING_META['request']['remote_ip'] + '\n' + LOGGING_META['request']['uri'],
    'result_template_02': LOGGING_META['result']['code'] + LOGGING_META['result']['text']
}

logging.config.dictConfig(LOGGING)

if __name__ == '__main__':
    logger = logging.getLogger('docker_cloud_manage')
    kwg = {'hehe': '12312'}
    logger.info('%(name)s is %(years)d years old.', {'name':'Tom', 'years':10})
